//
//  ZBLEhandleDataModel.m
//  ZRGP
//
//  Created by Jun on 2020/11/25.
//  Copyright © 2020 HWD. All rights reserved.
//

#import "ZBLEhandleDataModel.h"
#import "JCDataConvert.h"

@implementation ZBLEhandleDataModel

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (instancetype)initWith:(NSData *)data{
    
    self = [super init];
    if (self) {
        self.zpNum = [JCDataConvert oneByteToDecimalUint:[data subdataWithRange:NSMakeRange(0, 1)]];
        self.ackNum = [JCDataConvert oneByteToDecimalUint:[data subdataWithRange:NSMakeRange(1, 1)]];
        self.cmdNum = [JCDataConvert oneByteToDecimalUint:[data subdataWithRange:NSMakeRange(2, 1)]];
        NSInteger length = [JCDataConvert oneByteToDecimalUint:[data subdataWithRange:NSMakeRange(3, 1)]];
        if (self.ackNum == 0x78) {
           if (length>0) {
              self.valueNum = [JCDataConvert oneByteToDecimalUint:[data subdataWithRange:NSMakeRange(4, 1)]];
           }else{
              self.valueNum = 0x00;
           }
        }
        if (self.ackNum == 0x79) {
            if (length>1) {
                self.valueNum = [JCDataConvert twoBytesToDecimalUint:[data subdataWithRange:NSMakeRange(4, 2)]];
            }else{
                self.valueNum = 0x00;
            }
            
            if (length>1) {
                for (NSInteger i = 0; i<7; i++) {
                    if ((4+7*2)<[data length]) {
                        NSInteger number = [JCDataConvert twoBytesToDecimalUint:[data subdataWithRange:NSMakeRange(4+i*2, 2)]];
                        if (_cmdNum == 0x0f&&i==4) {
                            continue;
                        }
                        [self.dataArray addObject:@(number)];
                    }
                }
            }
        }
    }
    return self;
}

@end
